<?php
namespace Admin\Controller;

class MemberController extends AdminController
{
    //查询直属代理
    public function under()
    {
        $data = D('Data')->getMember(10, UID);
        $this->assign('_page', $data['page']);
        $this->assign('member', $data['member']);
        $this->display();
    }

    //查询所有代理
    public function index()
    {
        $data = D('Data')->getMember(10);
        // dump($data);die;
        $this->assign('_page', $data['page']);
        $this->assign('member', $data['member']);
        $this->display();
    }

    //查询单个
    // public function member_search()
    // {
    //     $search = I('post.search');
    //     if (empty($search)) {
    //         $this->error('请输入查询条件');
    //     } else {
    //         $member = D('Data')->member_search($search);
    //         if (!$member) {
    //             $this->error(D('Data')->getError());
    //         } else {
    //             $this->assign('member', $member);
    //         }
    //     }
    // }

    //删除
    public function del(){
        $id = I('id',0);
        empty($id) && $this->error('缺少参数');
        $res = M('member')->where(['id'=>$id])->delete();
        if($res) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    //添加
    public function add(){
        if(IS_GET){
            $this->assign('p1',I('id',1));
            if(UID != 1) $map['id'] = 8;
            $this->assign('group',M('group')->where($map)->select());
            $this->display();
        } else {
            $Member = D('Member');
            $res = $Member->update();
            if(!$res) {
                $this->error($Member->getError());
            } else {
                $this->success('添加成功');
            }
        }

    }

    //更新
    public function update(){
        if(!IS_POST){
            $id = I('id',0);
            empty($id) && $this->error('缺少参数');
            $data = M('member')->where(['id'=>$id])->find();
            $this->assign('group',M('group')->select());
            $this->assign('data',$data);
            $this->display();
        } else {
            // dump($_POST);die;
            $Member = D('Member');
            $m = $Member->update();
            if($m) {
                $this->success('修改成功');
            } else {
                $this->error($Member->getError());
            }
        }
    }


}